package com.xiaozhen.cmdb.controller;

import com.xiaozhen.cmdb.service.CmdbExampleService;
import com.xiaozhen.security.controller.BaseController;
import com.xiaozhen.common.core.domain.AjaxResult;
import com.xiaozhen.common.core.domain.cmdb.CmdbExample;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * HISTORY
 *
 * @author zhenweisheng
 * @date 2021/9/9 14:23
 * @description:
 */
@Api(tags = "Cmdb实例接口")
@RestController
@RequestMapping("cmdbExample")
public class CmdbExampleController extends BaseController {

    @Autowired
    private CmdbExampleService cmdbExampleService;

    @ApiOperation("查询实例列表")
    @GetMapping("/selectCmdbExampleList")
    public AjaxResult selectCmdbExampleList() {
        List<CmdbExample> exampleList = cmdbExampleService.selectCmdbExampleList();
        return AjaxResult.success(exampleList);
    }

    @ApiOperation("添加协议实例信息")
    @PostMapping("/addCmdbExample")
    public AjaxResult addCmdbExample(@Validated @RequestBody CmdbExample cmdbExample) {
        cmdbExample.setCreateBy(getCurrentUserId());
        cmdbExample.setCreateTime(LocalDateTime.now());
        return toAjax(cmdbExampleService.insertCmdbExample(cmdbExample));
    }

    @ApiOperation("修改协议实例信息")
    @PutMapping("/updateCmdbExample")
    public AjaxResult updateCmdbExample(@Validated @RequestBody CmdbExample cmdbExample) {
        cmdbExample.setUpdateBy(getCurrentUserId());
        cmdbExample.setUpdateTime(LocalDateTime.now());
        return toAjax(cmdbExampleService.updateCmdbExample(cmdbExample));
    }

    @ApiOperation("删除协议实例信息")
    @DeleteMapping("/{exampleId}")
    public AjaxResult remove(@PathVariable Long exampleId) {
        return toAjax(cmdbExampleService.deleteExampleById(exampleId));
    }
}
